<?
global $_skin;
global $user;
$e = $_skin["page"];
$id = $e["id"];
$t = $_skin["trail"][count($_skin["trail"])-1];
global $topurl;
$release = "`release`";



function _skin_nbsp($s) { return str_replace(" ","&nbsp;",$s); }

#ar_dump($params);
#echo "<pre>";print_r($_skin["trail"]);echo "</pre>";
global $skindir;
if (!$_skin["baseless"] && basename($skindir)=="main") {
	if ($t["fname"] || $params["fname"])
		echo "<base href='{$t["uri"]}{$e["id"]}/'>";
		else echo "<base href='{$t["uri"]}'>";
	}

include dirname(__FILE__)."/../fncs.php";

$replace = array();

#TITLE
$replace["__TITLE__"] = $e["title"];

#TITLE_H1
if ($e["title"]) { $replace["__TITLE_H1__"] = "<h1>{$e["title"]}</h1>"; }
else { $replace["__TITLE_H1__"] = ""; }

#TOPURL
$replace["__TOPURL__"] = $topurl;

#LINKS
$fid = 0;
$furl = "$topurl";
$where = "folder = 0 and folderid = $fid"; #".$db->sqlint($e["folderid"]);
ob_start();

$ids = array(0);
/*if ($e["folderid"] == $fid) {
	$r = _skin_path($where,$id);
	foreach ($r as $ee) $ids[]=$ee["id"];
	}*/
$r = _skin_tree($where,$ids);

#$r = _skin_tree($where,array(0));
foreach ($r as $ee) {
	echo str_repeat("&nbsp",$ee["level"]*4)."<a "._skin_link($ee,$furl).">"._skin_nbsp($ee["name"])."</a><br>";
	}
$v = ob_get_contents();
ob_end_clean();
$replace["__LINKS__"] = $v;

#CATEGORIES
ob_start();

$r = $db->query("select value,count(*) as cnt from tags group by value order by cnt desc limit 12");
foreach ($r as $e) {
    echo "<a href='$topurl/tags/{$e["value"]}'>{$e["value"]}</a> ({$e["cnt"]})<br>";
}

echo "<a href='$topurl/tags/'>... <b>more</b> tags!</a>";

$v = ob_get_contents();
ob_end_clean();
$replace["__CATEGORIES__"] = $v;


/*
$fid = 1;
$furl = "$topurl/projects";
$where = "folder = 0 and folderid = $fid"; #".$db->sqlint($e["folderid"]);
ob_start();

$ids = array(0);
if ($e["folderid"] == $fid) {
	$r = _skin_path($where,$id);
	foreach ($r as $ee) $ids[]=$ee["id"];
	}

$r = _skin_tree($where,$ids);

#$r = _skin_tree($where,array(0));
foreach ($r as $ee) {
	echo str_repeat("&nbsp",$ee["level"]*4)."<a "._skin_link($ee,$furl).">"._skin_nbsp($ee["name"])."</a><br>";
	}

$v = ob_get_contents();
ob_end_clean();
$replace["__CATEGORIES__"] = $v;
*/

#USERS_TITLE
$v = "members";
if ($user["id"]) $v = $user["title"];
$replace["__USERS_TITLE__"] = $v;


#USERS
ob_start();
#echo "&nbsp;<br>";
#echo "<hr>";
if ($user["id"]) {
	echo "<a href='$topurl/members/user/edit.php'>Your Profile</a><br>";
	echo "<a href='$topurl/members/projects/'>Your Projects</a><br>";
	echo "<a href='$topurl/members/projects/new/new.php'>Add a Project</a><br>";
	#echo "<hr>";
	echo "&nbsp;<br><a href='$topurl/login/logout.php'>Sign Out</a><br>";
} else { 
	#echo "<a href='$topurl/users/'>Sign In</a><br>";
	echo "<form method='post' action='$topurl/login/login.php'>";
	echo "User Name:<br>";
	echo "<input type=text name=name size=10><br>";
	echo "Password:<br>";
	echo "<input type=password name=passwd size=10><br>";
	echo "<input type='submit' value='Sign In'>";
	echo "</form>";
	echo "New members:<br>";
	echo "<a href='$topurl/login/new.php'>Sign Up</a><br>";
	}

$v = ob_get_contents();
ob_end_clean();
$replace["__USERS__"] = $v;


#SPOTLIGHT
ob_start();
#$sids = array(33); # outerspace
#$sids = array(123,126,124); #dynamite, quido, pylonoid
#$sids = array(120); #the witchs yarn
#$sids = array(181,163); #angry drunken dwarves, galaxy mage
#$sids = array(238,235,237); #pyweek2: nelly's garden, funnyboat, 20000 light years
#$sids = array(306,300,308); #pyweek3: typus pokus, wijafjord, bouncy
// $sids = array(406,415); #pyweek4: barbie seahorse, woody tigerbalm
$sids = array(487,498); #pyweek5: Disk Field, 555-BOOM
shuffle($sids);
$sid = $db->sqlint($sids[0]);
$r = $db->query("select project.id as pid,project.node_id,project.title,project.image,$release.datetimeon,$release.title as version,$release.id as rid from project,$release where $release.project_id = project.id and project.id = $sid order by $release.datetimeon desc limit 1"); 
inc_inclib("image.php");

foreach ($r as $ee) {
// 	echo "<a href=\"$topurl/projects/{$ee["node_id"]}/{$ee["pid"]}/?release_id={$ee["rid"]}\">";
	echo "<a href=\"$topurl/project/{$ee["pid"]}/?release_id={$ee["rid"]}\">";
	
	echo _skin_nbsp(date("M j, Y",strtotime($ee["datetimeon"])));
	echo "<br>";
	$fname = "$topdir/shots/{$ee["image"]}";
	if ($ee["image"] != "" && file_exists($fname)) {
	$img = image_thumb($fname,124,93,"jpg",50);
	list($iw,$ih) = getimagesize("$topdir/thumb/$img");
	echo "<img src='$topurl/thumb/$img' width=$iw height=$ih>";
	echo "<br>"; } else echo "<img src='$topurl/content/noimage.gif' width=124 height=93><br>";
	echo "{$ee["title"]} - {$ee["version"]}";
	echo "</a>"; #<br><br>";
	}

$v = ob_get_contents();
ob_end_clean();
$replace["__SPOTLIGHT__"] = $v;



#PROJECTS
ob_start();

$r = $db->query("select project.id as pid,project.node_id,project.title,project.image,$release.datetimeon,$release.title as version,$release.id as rid from project,$release where $release.project_id = project.id order by $release.datetimeon desc limit 36"); 
inc_inclib("image.php");

$done = array();

foreach ($r as $ee) {
    if (count($done) == 12) { continue; }
    if (in_array($ee["pid"],$done)) { continue; }
    $done[] = $ee["pid"];
// 	echo "<a href=\"$topurl/projects/{$ee["node_id"]}/{$ee["pid"]}/?release_id={$ee["rid"]}\">";
	echo "<a href=\"$topurl/project/{$ee["pid"]}/?release_id={$ee["rid"]}\">";
	
	echo _skin_nbsp(date("M j, Y",strtotime($ee["datetimeon"])));
	echo "<br>";
	$fname = "$topdir/shots/{$ee["image"]}";
	if ($ee["image"] != "" && file_exists($fname)) {
	$img = image_thumb($fname,124,93,"jpg",50);
	list($iw,$ih) = getimagesize("$topdir/thumb/$img");
	echo "<img src='$topurl/thumb/$img' width=$iw height=$ih>";
	echo "<br>"; } else echo "<img src='$topurl/content/noimage.gif' width=124 height=93><br>";
	echo "{$ee["title"]} - {$ee["version"]}";
	echo "</a><br><br>";
	}

$v = ob_get_contents();
ob_end_clean();
$replace["__PROJECTS__"] = $v;

#read in the skin
ob_start();
include dirname(__FILE__)."/index.html";
$content = ob_get_contents();
ob_end_clean();

#replace the changes
foreach ($replace as $k=>$v)
	$content = str_replace($k,$v,$content);

#output content
echo $content;

?>
